from pathlib import Path
import csv
from datetime import datetime

import matplotlib.pyplot as plt

path = Path("death_valley_2021_simple.csv")
lines = path.read_text().splitlines()

reader = csv.reader(lines)
header_row = next(reader)

# for index, column_header in enumerate(header_row):
#     print(index,column_header)

# 提取日期、最高温度和最低温度
dates, highs, lows = [], [], []
for row in reader:
    current_date = datetime.strptime(row[2], "%Y-%m-%d")
    try:
        high = int(row[3])
        low = int(row[4])
    except ValueError:
        print(f"Missing data for {current_date}")
    else:
        dates.append(current_date)
        highs.append(high)
        lows.append(low)

# 根据数据绘图
plt.style.use("classic")
fig, ax = plt.subplots()
ax.plot(dates, highs, color="red")
ax.plot(dates, lows, color="blue")

# 设置绘图的格式
ax.set_title("Daily High and Low Temperatures, 2021\nDeath Valley,CA", fontsize=20)
ax.set_xlabel("", fontsize=16)
fig.autofmt_xdate()
ax.set_ylabel("Temperature (F)", fontsize=16)
# 设置 y 轴数据值范围
ax.set_ylim(bottom=0, top=150)  # 例如，设置 y 轴范围为 0°F 至 120°F
ax.tick_params(labelsize=16)

plt.show()
